﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Cityship
{
    class Tile
    {
        private Point _location;
        public Point Location { get { return _location; } }

        public bool HasFloor;


        private TileType _type;
        public TileType Type { get { return _type; } }

        private string _spriteName;
        public string SpriteName { get { return _spriteName; } }

        private float _rotation;
        public float Rotation { get { return _rotation; } set { _rotation = value; } }

        private float _pressure;
        public float Pressure { get { return _pressure; } set { _pressure = value; if (_pressure < 0)_pressure = 0; } }

        public Vector2 AirFlow;

        public Tile()
        {
            Change(TileType.Nothing, "default");
        }

        public Tile(TileType tile, string tileSprite, Point point)
        {
            Change(tile, tileSprite);
            _location = point;

            Pressure = (float)Utility.rand.NextDouble();
            AirFlow = Vector2.Zero;
        }

        public void SetSpriteName(string newSpriteName)
        {
            if (SpriteSet.GetTileSet(_type).ContainsKey(newSpriteName))
            {
                _spriteName = newSpriteName;
            }
            else
            {
                _spriteName = "default";
            }
        }

        public void Change(TileType newType, string tileSprite)
        {
            _type = newType;
            _spriteName = tileSprite;
            Rotation = 0f;
        }

        public void ChangePressure(float rate)
        {
            float newPressure = Pressure + rate;
            if (newPressure > 0f & newPressure < 1f)
                Pressure += rate;
            else
                Pressure = Convert.ToInt16(Pressure);
        }

    }

    static class TileAdjacency
    {
        public static void Recalculate(Deck deck)
        {
            Recalculate(deck, 0, deck.GetWidth,0,deck.GetHeight); //Calculate entire deck.
        }
        public static void Recalculate(Deck deck, int xPos, int yPos)
        {
            int xPosNew = xPos;
            int yPosNew = yPos;
            //Constrain calculation space to valid deck grid.
            if (xPos < 1)
                xPosNew = 1;
            if (xPos > deck.GetWidth)
                xPosNew = deck.GetWidth;
            if (yPos < 1)
                yPosNew = 1;
            if (yPos > deck.GetHeight)
                yPosNew = deck.GetHeight;

            Recalculate(deck, xPosNew - 1, xPosNew + 1, yPosNew - 1, yPosNew + 1); //Calculate 3x3 square around original X/Y coordinate
        }

        public static void Recalculate(Deck deck, int xPos, int xPosMax, int yPos, int yPosMax)
        {
            Random rand = new Random();
            for (int x = xPos; x <= xPosMax; x++)
            {
                for (int y = yPos; y <= yPosMax; y++)
                {
                    if (x < 0 | x >= deck.GetWidth)
                        break;
                    if (y < 0 | y >= deck.GetHeight)
                        break;

                    Tile tile = deck.TileGrid[x, y];
                    if (tile.Type == TileType.Wall)
                    {

                        bool north = false, south = false, east = false, west = false;

                        if (y - 1 >= 0 && deck.TileGrid[x, y - 1].Type == TileType.Wall)
                            north = true; //North

                        if (y + 1 < deck.GetHeight && deck.TileGrid[x, y + 1].Type == TileType.Wall)
                            south = true; //South

                        if (x - 1 >= 0 && deck.TileGrid[x - 1, y].Type == TileType.Wall)
                            east = true; //East

                        if (x + 1 < deck.GetWidth && deck.TileGrid[x + 1, y].Type == TileType.Wall)
                            west = true; //West

                        int count = 0;

                        if (north)
                            count++;
                        if (south)
                            count++;
                        if (east)
                            count++;
                        if (west)
                            count++;

                        tile.Rotation = 0f;
                        switch (count)
                        {
                            case 0:
                                tile.SetSpriteName("column");
                                break;
                            case 1:
                                tile.SetSpriteName("cap");
                                if (north)
                                {
                                    tile.Rotation = MathHelper.Pi;
                                }
                                else if (south)
                                {
                                    // Default Rotation
                                }
                                else if (east)
                                {
                                    tile.Rotation = MathHelper.PiOver2;
                                }
                                else if (west)
                                {
                                    tile.Rotation = -MathHelper.PiOver2;
                                }
                                break;
                            case 2:
                                if (north && south)
                                {
                                    tile.SetSpriteName("straight");
                                }
                                else if (east && west)
                                {
                                    tile.SetSpriteName("straight");
                                    tile.Rotation = MathHelper.PiOver2;
                                }
                                else
                                {
                                    tile.SetSpriteName("corner");
                                    if (north)
                                    {
                                        tile.Rotation += MathHelper.Pi;
                                        if (west)
                                        {
                                            tile.Rotation += MathHelper.PiOver2;
                                        }
                                    }
                                    else
                                    {
                                        if (east)
                                        {
                                            tile.Rotation += MathHelper.PiOver2;
                                        }
                                    }

                                }

                                break;
                            case 3:
                                tile.SetSpriteName("tri");
                                if (!north)
                                {
                                    //Default Rotation
                                }
                                else if (!south)
                                {
                                    tile.Rotation = MathHelper.Pi;
                                }
                                else if (!east)
                                {
                                    tile.Rotation = -MathHelper.PiOver2;
                                }
                                else if (!west)
                                {
                                    tile.Rotation = MathHelper.PiOver2;
                                }
                                break;
                            case 4:
                                tile.SetSpriteName("cross");
                                break;
                            default:
                                break;
                        }


                    }
                }
            }
            //List<Tile> AdjacentTiles = new List<Tile> { };
        }
    }

    
}
